<template>
    <el-row style="min-height: 100vh;">
        <el-col :lg="16" :md="12" style="background-color: #f1939c;" class="flex items-center justify-center flex-col">
            <div class="font-bold text-5xl" style="color:#1b38c9;margin-bottom: 5px;">欢迎进入</div>
            <!-- <div>这个是由四岁在2023/4/23号开始v3写的后台管理</div> -->
            <div>密码和用户名默认都是<span style="color:#14d9ed;font-weight: 900;font-size: 40px;">admin</span></div>

        </el-col>
        <el-col :lg="8" :md="12" style="background-color: #ffffff;" class="flex items-center justify-center flex-col">
            <h2 class="font-bold text-5xl" style="color:#080808;margin-bottom: 5px;">欢迎登录</h2>
            <el-form ref="FormRef" :model="form" style="width:250px" :rules="rules">
                <el-form-item prop="usename">
                    <el-input v-model="form.usename" placeholder="请输入用户名">
                        <template #prefix>
                            <el-icon class="el-input__icon">
                                <UserFilled />
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item prop="paseword">
                    <el-input type="password" v-model="form.paseword" placeholder="请输入密码" show-password>
                        <template #prefix>
                            <el-icon class="el-input__icon">
                                <Lock />
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item>
                    <el-button style="width:250px;border-radius: 20px;" type="primary" @click="onSubmit"
                        :loading="loading">登 录</el-button>
                </el-form-item>
            </el-form>
        </el-col>
    </el-row>
</template>

<script setup>
import { reactive, ref,onMounted,onBeforeMount } from 'vue'
import { login } from '~/api/manager'
import { useRouter } from "vue-router";
import { success } from '~/composables/util'
import { useStore } from 'vuex'
import { setToken } from '~/composables/auth'

const router = useRouter()
const store=useStore()
const loading = ref(false)

const form = reactive({
    usename: '',
    paseword: ''
})
const rules = {
    usename: [
        { required: true, message: '用户名不能为空', trigger: 'blur' },
    ],
    paseword: [
        { required: true, message: '密码不能为空', trigger: 'blur' },
    ]
}
const FormRef = ref(null)
//登录按钮
const onSubmit = () => {
    FormRef.value.validate((valid) => {
        if (!valid) {
            return false
        }
        loading.value = true
        login(form.usename, form.paseword).then(res => {
            success('登录成功')
            setToken(res.token)
            router.push('/')
        }).finally(() => {
            loading.value = false
        })
    })
}
//监听回车按钮
function onKeyUp(e){
    if(e.key=="Enter") onSubmit()
}
//监听键盘事件
onMounted(()=>{
    document.addEventListener("keyup",onKeyUp)
})
onBeforeMount(()=>{
    document.addEventListener("keyup",onKeyUp)
})
</script>